/**
 * Created by lional on 16/10/2.
 */

jGame.Layout = ccui.Layout.extend(/** @lends jGame.Layout# */{

    _touchTime:null,
    _controller:null,
    _isLongClickEnabled: null,
    _longClickAction: null,
    _longClickName: null,
    _sendLongClick: null,
    _oneByOneCallbackListener: null,
    _allAtOnceCallbackListener: null,

    ctor: function () {
        this._super();
        //cc.log("jGame.Layout#ctor()");
    },

    setController: function(controller){
        this._controller = controller;
    },

    getController: function(){
        return this._controller;
    },

    _checkLongClickToucheBegan: function (sender, type) {
        if(type == ccui.Widget.TOUCH_BEGAN) {
            sender._touchTime = new Date().getTime();
            this.stopAction(this._longClickAction);

            var widget = this;
            var longClickCallBack = function (sender) {
                if(widget["_controller"]) {
                    if(typeof widget["_controller"]["trace"] === "function")widget["_controller"]["trace"](sender, ccui.Widget.TOUCH_ENDED);
                    if(typeof widget["_controller"][widget["_longClickName"]] === "function")widget["_controller"][widget["_longClickName"]](sender, ccui.Widget.TOUCH_ENDED);
                    widget._sendLongClick = true;
                }
            }
            this._longClickAction = cc.sequence(
                cc.delayTime(jGame.LONG_CLICK_TIME/1000),
                cc.callFunc(longClickCallBack, this)  // If you want to pass a optional value, like "Hi!", then you should pass 'this' too
            );
            this.runAction(this._longClickAction);
            widget._sendLongClick = false;
        }else if(type == ccui.Widget.TOUCH_ENDED) {
            setTimeout(function () {
                sender._touchTime = 0;
            }, 0)
        }
    },

    isLongClick: function () {
        if(this._touchTime) {
            var timestamp = new Date().getTime();
            if(timestamp - this._touchTime >= jGame.LONG_CLICK_TIME) {
                return true;
            }
        }
        return false;
    },
    
    setLongClickEnabled: function (enabled) {
        this._isLongClickEnabled = enabled?true:false;
    },

    setTouchCallbackName: function (callBackName) {
        this._longClickName = callBackName;
        if(callBackName === undefined)
        {
            this.addTouchEventListener(callBackName);
            return;
        }
        var widget = this;
        var callBack = function(sender, type){
            if(type == ccui.Widget.TOUCH_BEGAN) {
                if (widget._isLongClickEnabled && typeof widget["_checkLongClickToucheBegan"] === "function") {
                    widget["_checkLongClickToucheBegan"](sender, type);
                }
            }else if(type == ccui.Widget.TOUCH_MOVED) {
                if(widget._longClickAction) {
                    widget.stopAction(widget._longClickAction);
                }
            }
            if(widget._sendLongClick) {
                widget._sendLongClick = false;
            }else {
                if(typeof widget["_controller"] === "object" && typeof widget["_controller"][callBackName] === "function"){
                    if(typeof widget["_controller"]["trace"] === "function")widget["_controller"]["trace"](sender, type);
                    widget["_controller"][callBackName](sender, type);
                }
                else if(typeof widget[callBackName] === "function")
                    widget[callBackName](sender, type);
            }
        };
        this.setTouchEnabled(true);
        this.addTouchEventListener(callBack);
    },

    setOneByOneCallbackName:function (touchBeganCallBackName, touchMovedCallBackName, touchEndedCallBackName, touchCancelledCallBackName, touchSwallowEnabled) {
        var widget = this;
        var onTouchBegan = function (touch, event) {
            if(typeof widget["_controller"] === "object" && typeof widget["_controller"][touchBeganCallBackName] === "function"){
                if(typeof widget["_controller"]["trace"] === "function")widget["_controller"]["trace"](widget, touch, event);
                var result =  widget["_controller"][touchBeganCallBackName](touch, event);
                return result;
            }
            else if(typeof widget[touchBeganCallBackName] === "function")
                return widget[touchBeganCallBackName](touch, event);
        }
        var onTouchMoved = function (touch, event) {
            if(typeof widget["_controller"] === "object" && typeof widget["_controller"][touchMovedCallBackName] === "function"){
                if(typeof widget["_controller"]["trace"] === "function")widget["_controller"]["trace"](widget, touch, event);
                var result =  widget["_controller"][touchMovedCallBackName](touch, event);
                return result;
            }
            else if(typeof widget[touchMovedCallBackName] === "function")
                return widget[touchMovedCallBackName](touch, event);
        }
        var onTouchEnded = function (touch, event) {
            if(typeof widget["_controller"] === "object" && typeof widget["_controller"][touchEndedCallBackName] === "function"){
                if(typeof widget["_controller"]["trace"] === "function")widget["_controller"]["trace"](widget, touch, event);
                var result =  widget["_controller"][touchEndedCallBackName](touch, event);
                return result;
            }
            else if(typeof widget[touchEndedCallBackName] === "function")
                return widget[touchEndedCallBackName](touch, event);
        }
        var onTouchCancelled = function (touch, event) {
            if(typeof widget["_controller"] === "object" && typeof widget["_controller"][touchCancelledCallBackName] === "function"){
                if(typeof widget["_controller"]["trace"] === "function")widget["_controller"]["trace"](widget, touch, event);
                var result =  widget["_controller"][touchCancelledCallBackName](touch, event);
                return result;
            }
            else if(typeof widget[touchCancelledCallBackName] === "function")
                return widget[touchCancelledCallBackName](touch, event);
        }

        if(touchSwallowEnabled) {
            touchSwallowEnabled = true;
        }
        else {
            touchSwallowEnabled = false;
        }

        if(this._oneByOneCallbackListener) {
            cc.eventManager.removeListener(this._oneByOneCallbackListener);
        }

        this._oneByOneCallbackListener = cc.EventListener.create({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: touchSwallowEnabled,
            onTouchBegan: onTouchBegan,
            onTouchMoved: onTouchMoved,
            onTouchEnded: onTouchEnded,
            onTouchCancelled: onTouchCancelled
        });

        cc.eventManager.addListener(this._oneByOneCallbackListener, this);
    },

    setAllAtOnceCallbackName:function (touchBeganCallBackName, touchMovedCallBackName, touchEndedCallBackName, touchCancelledCallBackName) {
        var widget = this;
        var onTouchBegan = function (touch, event) {
            if(typeof widget["_controller"] === "object" && typeof widget["_controller"][touchBeganCallBackName] === "function"){
                if(typeof widget["_controller"]["trace"] === "function")widget["_controller"]["trace"](widget, touch, event);
                var result =  widget["_controller"][touchBeganCallBackName](touch, event);
                return result;
            }
            else if(typeof widget[touchBeganCallBackName] === "function")
                return widget[touchBeganCallBackName](touch, event);
        }
        var onTouchMoved = function (touch, event) {
            if(typeof widget["_controller"] === "object" && typeof widget["_controller"][touchMovedCallBackName] === "function"){
                if(typeof widget["_controller"]["trace"] === "function")widget["_controller"]["trace"](widget, touch, event);
                var result =  widget["_controller"][touchMovedCallBackName](touch, event);
                return result;
            }
            else if(typeof widget[touchMovedCallBackName] === "function")
                return widget[touchMovedCallBackName](touch, event);
        }
        var onTouchEnded = function (touch, event) {
            if(typeof widget["_controller"] === "object" && typeof widget["_controller"][touchEndedCallBackName] === "function"){
                if(typeof widget["_controller"]["trace"] === "function")widget["_controller"]["trace"](widget, touch, event);
                var result =  widget["_controller"][touchEndedCallBackName](touch, event);
                return result;
            }
            else if(typeof widget[touchEndedCallBackName] === "function")
                return widget[touchEndedCallBackName](touch, event);
        }
        var onTouchCancelled = function (touch, event) {
            if(typeof widget["_controller"] === "object" && typeof widget["_controller"][touchCancelledCallBackName] === "function"){
                if(typeof widget["_controller"]["trace"] === "function")widget["_controller"]["trace"](widget, touch, event);
                var result =  widget["_controller"][touchCancelledCallBackName](touch, event);
                return result;
            }
            else if(typeof widget[touchCancelledCallBackName] === "function")
                return widget[touchCancelledCallBackName](touch, event);
        }

        if(this._allAtOnceCallbackListener) {
            cc.eventManager.removeListener(this._allAtOnceCallbackListener);
        }

        this._allAtOnceCallbackListener = cc.EventListener.create({
            event: cc.EventListener.TOUCH_ALL_AT_ONCE,
            onTouchesBegan: onTouchBegan,
            onTouchesMoved: onTouchMoved,
            onTouchesEnded: onTouchEnded,
            onTouchesCancelled: onTouchCancelled
        });

        cc.eventManager.addListener(this._allAtOnceCallbackListener, this);
    },

    _createJGameInstance: function () {
        return new jGame.Layout();
    },

    _copyJGameProperties: function (model) {
        this._copyProperties(model);
        this.setController(model.getController());
    },

    _copyJGameChildren: function (model) {
        var widgetChildren = model.getChildren();
        for (var i = 0; i < widgetChildren.length; i++) {
            var locChild = widgetChildren[i];
            if (locChild instanceof cc.Node)
                this.addChild(jGame.clone(locChild));
        }
    },

});